/*
 * berton
 */
package com.pitop.t.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.TpermissionException;
import com.pitop.t.dao.TpermissionDAO;
import com.pitop.t.entity.Permission;
import com.pitop.t.entity.PermissionState;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.*;

@Service("tpermissionSV")
@Slf4j
public class TpermissionSVImpl extends BaseSVImpl<Permission, Long> implements TpermissionSV {

    @Autowired
    private TpermissionDAO tpermissionDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return tpermissionDAO;
    }

    /**
    * 保存account对象
    *
    * @param entity 实体
    * @throws BaseException
    */
    @Override
    public void save(Permission entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象Tpermission
     * @param permissionId 
     * @return Permission
     */
    @Override
    public Permission load(java.lang.Integer permissionId) {
           if(permissionId==null){
                 throw new TpermissionException(BaseException.BaseExceptionEnum.Ilegal_Param);
           }

            Map<String,Object> param=new HashMap<>();
            param.put("permissionId",permissionId);
            return tpermissionDAO.load(param);
    }
    /**
     * 加载一个对象Tpermission 通过permissionId
     * @param permissionId 
     * @return Permission
     */
    @Override
    public Permission loadByPermissionId(java.lang.Integer permissionId) {
            if(permissionId==null){
                 throw new TpermissionException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return null;
    }


    /**
    * 根据主键permissionId,oldStates 共同更新 Permission 的状态到newState状态
    *
    * @param permissionId 
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateByPermissionId(java.lang.Integer permissionId, PermissionState newState, PermissionState... oldStates){
        if(permissionId==null){
            throw new TpermissionException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new TpermissionException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new TpermissionException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        tpermissionDAO.updateStateByPermissionId(permissionId,new Date(),newState,oldStates);
    }

    /**
    * 根据主键permissionId 更新 Permission 的状态到另一个状态
    *
    * @param permissionId 
    * @param state 状态
    */
    @Override
    public void updateByPermissionId(java.lang.Integer permissionId,PermissionState state){
            if(permissionId==null){
               throw new TpermissionException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          tpermissionDAO.updateByPermissionId(permissionId,state,new Date());
    }


    /**
     * 删除对象Tpermission
     * @param permissionId 
     * @return Permission
     */
    @Override
    public void delete(java.lang.Integer permissionId) {
            if(permissionId==null){
                throw new TpermissionException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            Map<String,Object> param=new HashMap<>();
            param.put("permissionId",permissionId);
            tpermissionDAO.delete(param);
    }



    /**
    * 查询Tpermission分页
    *
    * @param tpermission  对象
    * @param offset 查询开始行
    * @param limit  查询行数
    * @return List<Permission>
    */
    @Override
    public List<Permission> list(Permission tpermission, int offset, int limit) {
            if (offset < 0) {
               offset = 0;
            }

            if (limit < 0) {
               limit = Page.limit;
            }

            Map<String, Object> map = null;
            if (tpermission != null) {
               map = JSON.parseObject(JSON.toJSONString(tpermission, SerializerFeature.WriteDateUseDateFormat));
            } else {
               map = new HashMap<>();
            }
        return tpermissionDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public  int count(Permission tpermission){
        Map<String, Object> map = null;
        if (tpermission != null) {
           map = JSON.parseObject(JSON.toJSONString(tpermission, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
       return tpermissionDAO.count(map);
     }

    /**
     * 查询Tpermission分页
     *
     * @param permissionId  
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Permission>
     */
    @Override
    public List<Permission> list(java.lang.Integer permissionId, int offset, int limit) {
            if (offset < 0) {
              offset = 0;
            }

            if (limit < 0) {
              limit = Page.limit;
            }
            Map<String,Object> param=new HashMap<>();
            param.put("permissionId",permissionId);
            return tpermissionDAO.list(param,new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Integer permissionId) {
            Map<String,Object> param=new HashMap<>();
            param.put("permissionId",permissionId);
            return tpermissionDAO.count(param);
    }

    @Override
    public Map<String, List<Permission>> getPermissionMapByUserId(Integer userId) {
        List<Permission> permissionList = tpermissionDAO.getPermissionList(userId);

        List<Permission> menuList = new ArrayList<>();
        Map<Integer, Permission> map = new HashMap<>();

        // 筛选目录
        for (Permission permission : permissionList) {
            if (permission.getType() != 3 && permission.getPid() == 0L) {
                map.put(permission.getPermissionId(), permission);
                menuList.add(permission);
            }
        }

        // 封装菜单
        for (Permission permission : permissionList) {
            if (permission.getType() != 3 && map.get(permission.getPid()) != null) {
                Permission parent = map.get(permission.getPid());
                parent.getChildren().add(permission);
            }
        }

        Map<String,List<Permission>> resultMap = new HashMap<>(2);
        resultMap.put("menuList", menuList);
        resultMap.put("permissionList", permissionList);

        return resultMap;
    }

    @Override
    public Page<Permission> getListByPage(int currentNum, int pageSize, String name) {

        Example cond = new Example(Permission.class);
        if (!StringUtils.isEmpty(name)) {
            try {
                name = new String(name.getBytes("iso8859-1"),"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            cond.createCriteria().andLike("name",name);
        }
        Page<Permission> page = new Page<>();
        page.setCurPage(currentNum);
        page.setPageSize(pageSize);
        //List<Permission> list = this.permissionMapper.selectByExample(cond);
        //page.setVoList(list);
        return  page;
    }

    @Override
    public List<Permission> getPermissionWithoutButton() {
        return null;
    }

    @Override
    public List<Permission> getPermissionListByRoleId(int roleId) {
        return null;
    }

}
